package main

import (
	"crypto/md5"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
)

func naturalLanguage() {
	appid := "585394c2"
	apikey := "9100aa60ee9184d07caab9e6fa6ce044"
	postUrl := "http://ltpapi.xfyun.cn/v1/srl"
	curtime := strconv.FormatInt(time.Now().Unix(), 10)

	param := make(map[string]string)
	param["type"] = "dependent"
	tmp, _ := json.Marshal(param)
	base64Param := base64.StdEncoding.EncodeToString(tmp)

	w := md5.New()
	io.WriteString(w, apikey+curtime+base64Param)
	checksum := fmt.Sprintf("%x", w.Sum(nil))

	data := url.Values{}
	data.Add("text", "我今天很开心")
	body := data.Encode()

	client := &http.Client{}
	req, _ := http.NewRequest("POST", postUrl, strings.NewReader(body))
	req.Header.Set("X-Appid", appid)
	req.Header.Set("X-CurTime", curtime)
	req.Header.Set("X-Param", base64Param)
	req.Header.Set("X-CheckSum", checksum)
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	res, _ := client.Do(req)
	defer res.Body.Close()
	res_body, _ := ioutil.ReadAll(res.Body)
	fmt.Print(string(res_body))
}

func main() {
	naturalLanguage()
}
